/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "HierachyWindow.h"

#include "GameBuilder.h"

namespace PQBuilder
{
	HierachyTree::HierachyTree(QWidget *parent)
	{
		this->setHeaderLabel(QObject::tr("Object Hierachy"));
		_rootItems[HRT_RENDER_SETTING] = new QTreeWidgetItem((QTreeWidget*)0, QStringList(QObject::tr("Render Settings")));
		_rootItems[HRT_TERRAIN_GROUP] = new QTreeWidgetItem((QTreeWidget*)0, QStringList(QObject::tr("Terrain Group")));
		
		this->insertTopLevelItem(HRT_RENDER_SETTING, _rootItems[HRT_RENDER_SETTING]);
		this->insertTopLevelItem(HRT_TERRAIN_GROUP, _rootItems[HRT_TERRAIN_GROUP]);

		_createTerrainMenu=new QMenu();
		
		QAction* createAction=_createTerrainMenu->addAction(QObject::tr("Create terrain slot"));
		_createTerrainMenu->addAction(QObject::tr("Import terrain from height map"));
		_createTerrainMenu->addAction(QObject::tr("Export height maps"));
		_createTerrainMenu->addAction(QObject::tr("Hide terrain group"));

		connect(this,SIGNAL(itemClicked(QTreeWidgetItem*, int)),this,SLOT(OnItemClicked(QTreeWidgetItem*, int)));
		connect(this,SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)),this,SLOT(OnItemDoubleClicked(QTreeWidgetItem*, int)));
		
		connect(createAction,SIGNAL(triggered(bool)),this,SLOT(OnCreateTerrainSlot()));
	}

	void HierachyTree::OnCreateTerrainSlot()
	{
		/*
		if(!_terrainCreateDialog){
			_terrainCreateDialog=new TerrainCreateDialog(this);
		}
		_terrainCreateDialog->show();
		*/
	}

	void HierachyTree::OnItemClicked(QTreeWidgetItem* item, int column)
	{
		if(item==_rootItems[HRT_TERRAIN_GROUP]){
			assert(!"Not Impl");
			//SEngine.getTerrainGroup();//use PQBuilder is better.
			return;
		}

		if(item==_rootItems[HRT_RENDER_SETTING]){
			//
			WidgetBase::selectRenderProperty();
			return;
		}

		if(item==_rootItems[HRT_RENDER_SETTING]){
			//
			return;
		}

		TreeItemIdList::iterator iter=_objectIds.find(item);
		if(iter!=_objectIds.end()){
			int id=iter->second;
			PQEngine::Object* object=PQEngine::Object::getObject(iter->second);
			if(object->getObjectType()==PQEngine::OBJECT_TYPE_TERRAIN_OBJECT){
				//PQEngine::SceneObject::getActive()->selectTerrain(id);
			}else if(object->getObjectType()==PQEngine::OBJECT_TYPE_GAME_OBJECT){
				PQEngine::SceneObject::getActive()->selectObject(iter->second);
			}
		}

	}

	void HierachyTree::OnItemDoubleClicked(QTreeWidgetItem* item, int column)
	{
		TreeItemIdList::iterator it=_objectIds.find(item);
		 if(it!=_objectIds.end()){
			 SGameBuilder.selectAndFrameObject(it->second);
		}
	}

	void HierachyTree::addTerrainGroup(PQEngine::TerrainGroupObject* object)
	{
		this->insertTopLevelItem(HRT_TERRAIN_GROUP, _rootItems[HRT_TERRAIN_GROUP]);
	}

	void HierachyTree::addTerrainObject(PQEngine::TerrainObject* object)
	{
		QTreeWidgetItem* item =new QTreeWidgetItem(_rootItems[HRT_TERRAIN_GROUP], QStringList(QObject::tr("Terrain")));
		_rootItems[HRT_TERRAIN_GROUP]->addChild(item);

		addItem(object->getId(),item);

	}

	void HierachyTree::addItem(int id,QTreeWidgetItem* item)
	{
		_objectItems.insert(TreeItemList::value_type(id,item));
		_objectIds.insert(TreeItemIdList::value_type(item,id));
	}

	HierachyTree::~HierachyTree() 
	{
		if(_createTerrainMenu!=NULL){
			delete _createTerrainMenu;
		}
	};

	void HierachyTree::removeChildGameObject(PQEngine::GameObject *parent,PQEngine::GameObject *child)
	{
		if(parent==NULL||child==NULL){
			return;
		}

		QTreeWidgetItem* parentItem=NULL;
		if(parent->isRoot()){
			parentItem=_rootItems[HRT_GAME_OBJECT];
		}else{
			TreeItemList::iterator iter= _objectItems.find(parent->getId());
			if(iter!=_objectItems.end()){
				parentItem=iter->second;
			}
		}

		if(parentItem!=NULL){
			TreeItemList::iterator it= _objectItems.find(child->getId());
			if(it!=_objectItems.end()){
				parentItem->removeChild(it->second);
				_objectItems.erase(it);
			}
		}
	}

	void HierachyTree::addChildGameObject(PQEngine::GameObject *parent,PQEngine::GameObject *child)
	{
		if(parent==NULL||child==NULL){
			return;
		}

		QTreeWidgetItem* parentItem=NULL;
		if(parent->isRoot()){
			parentItem=_rootItems[HRT_GAME_OBJECT];
		}else{
			TreeItemList::iterator iter= _objectItems.find(parent->getId());
			if(iter!=_objectItems.end()){
				parentItem=iter->second;
			}
		}

		if(parentItem!=NULL){
			QTreeWidgetItem* item =new QTreeWidgetItem(this, QStringList(QString::fromStdString(child->getName())));
			parentItem->addChild(item);
			addItem(child->getId(),item);
		}

	}

	void HierachyTree::addGameObject(PQEngine::GameObject *obj)
	{
		if(!obj){
			return;
		}
		if(obj->isRoot()){
			_rootItems[HRT_GAME_OBJECT] = new QTreeWidgetItem(QStringList(QString::fromStdString(obj->getName())));
			_rootItems[HRT_GAME_OBJECT]->setExpanded(true);
			this->insertTopLevelItem(HRT_GAME_OBJECT, _rootItems[HRT_GAME_OBJECT]);
		}else{
			//find paren item,then insert it.
			if(obj->isTerrain()){
				QTreeWidgetItem* parentItem=_rootItems[HRT_TERRAIN_GROUP];
				QTreeWidgetItem* item =new QTreeWidgetItem(QStringList(QString::fromStdString(obj->getName())));
				parentItem->addChild(item);
				addItem(obj->getId(),item);
			}else{
				QTreeWidgetItem* parentItem=_rootItems[HRT_GAME_OBJECT];
				QTreeWidgetItem* item =new QTreeWidgetItem(QStringList(QString::fromStdString(obj->getName())));
				parentItem->addChild(item);
				addItem(obj->getId(),item);
			}
		}

	}

	void HierachyTree::selectGameObject(PQEngine::GameObject *object)
	{
		clearSelection();
		if(object!=NULL){
			TreeItemList::iterator iter= _objectItems.find(object->getId());
			if(iter!=_objectItems.end()){
				iter->second->setSelected(true);
			}
		}
	}

	void HierachyTree::mousePressEvent(QMouseEvent *evt)
	{
		QTreeWidget::mousePressEvent(evt);
		QTreeWidgetItem *item = itemAt(evt->pos());
		if(item==NULL){
			return;
		}

		if(evt->button()==Qt::RightButton){
			if(item==_rootItems[HRT_TERRAIN_GROUP]){
				_createTerrainMenu->popup(evt->globalPos());
			}
		}
	}

	HierachyWindow::HierachyWindow(QWidget *parent)
		: WidgetBase(parent)
	{
		_treeWidget = new HierachyTree(this);

		QVBoxLayout *boxlayout = new QVBoxLayout(this);
		boxlayout->setMargin(0);
		boxlayout->addWidget(_treeWidget);
		boxlayout->setStretch(0,0);
		boxlayout->setStretch(1,1);

		
	}

	HierachyWindow::~HierachyWindow()
	{
		
	}

	void HierachyWindow::OnAddChildGameObject(PQEngine::GameObject *parent,PQEngine::GameObject *child)
	{
		_treeWidget->addChildGameObject(parent,child);
	}

	void HierachyWindow::OnRemoveChildGameObject(PQEngine::GameObject *parent,PQEngine::GameObject *child)
	{
		_treeWidget->removeChildGameObject(parent,child);
	}

	

	void HierachyWindow::OnGameObjectCreated(PQEngine::GameObject* object)
	{
		_treeWidget->addGameObject(object);
	}

	void HierachyWindow::OnGameObjectDeleted(PQEngine::GameObject* object)
	{
	}

	void HierachyWindow::OnGameObjectSelected(PQEngine::GameObject *object)
	{
		_treeWidget->selectGameObject(object);
	}

	void HierachyWindow::OnGameObjectDeselected(PQEngine::GameObject* object)
	{
	}
}
